import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:gif/gif.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:jump_scape/js_ui/js_widget/js_style.dart';
import 'package:svgaplayer_flutter/svgaplayer_flutter.dart';

Widget jsImage(
  String path, {
  double? width,
  double? height,
  BoxFit fit = BoxFit.contain,
  Alignment alignment = Alignment.center,
  Color? color,
  BlendMode? colorBlendMode,
  EdgeInsets? padding,
  VoidCallback? onPressed,
  VoidCallback? onLongPress,
}) {
  Widget child;
  bool isSvga = path.endsWith('.svga');
  bool isGif = path.endsWith('.gif');

  if (path.startsWith('http')) {
    child = CachedNetworkImage(
      imageUrl: path,
      width: width,
      height: height,
      fit: fit,
      color: color,
      colorBlendMode: colorBlendMode,
      useOldImageOnUrlChange: true,
      memCacheWidth: (width ?? ScreenUtil().screenWidth).toInt(),
      fadeInDuration: Duration.zero,
      alignment: alignment,
      errorWidget: (context, url, error) {
        return Container(
          width: width,
          height: height,
          color: Color(0xFF231F20),
          alignment: Alignment.center,
          child: Text(error.toString(), style: jsTextStyle(font: JsTextFont.PoetsenOne, fontSize: 10.r, fontColor: Colors.white)),
        );
      },
    );
  } else if (path.startsWith('assets')) {
    if (isSvga) {
      child = SizedBox(width: width, height: height, child: SVGASimpleImage(assetsName: path));
    } else if (isGif) {
      child = Gif(image: AssetImage(path), width: width, height: height, autostart: Autostart.loop, fit: fit, color: color, colorBlendMode: colorBlendMode, alignment: alignment);
    } else {
      child = Image.asset(
        path,
        width: width,
        height: height,
        fit: fit,
        color: color,
        colorBlendMode: colorBlendMode,
        alignment: alignment,
        errorBuilder: (context, error, stackTrace) {
          return Container(
            width: width,
            height: height,
            color: Color(0xFF231F20),
            alignment: Alignment.center,
            child: Text(error.toString(), style: jsTextStyle(font: JsTextFont.PoetsenOne, fontSize: 10.r, fontColor: Colors.white)),
          );
        },
      );
    }
  } else {
    if (isSvga) {
      child = SizedBox(width: width, height: height, child: SVGASimpleImage(resUrl: path));
    } else if (isGif) {
      child = Gif(image: FileImage(File(path)), width: width, height: height, autostart: Autostart.loop, fit: fit, alignment: alignment, color: color, colorBlendMode: colorBlendMode);
    } else {
      child = Image.file(
        File(path),
        width: width,
        height: height,
        fit: fit,
        alignment: alignment,
        color: color,
        colorBlendMode: colorBlendMode,
        errorBuilder: (context, error, stackTrace) {
          return Container(
            width: width,
            height: height,
            color: Color(0xFF231F20),
            alignment: Alignment.center,
            child: Text(error.toString(), style: jsTextStyle(font: JsTextFont.PoetsenOne, fontSize: 10.r, fontColor: Colors.white)),
          );
        },
      );
    }
  }

  child = padding != null ? Padding(padding: padding, child: child) : child;

  if (onPressed != null || onLongPress != null) {
    return InkWell(highlightColor: Colors.transparent, hoverColor: Colors.transparent, splashColor: Colors.transparent, onTap: onPressed, onLongPress: onLongPress, child: child);
  } else {
    return child;
  }
}
